package vben.init;

import cn.hutool.core.util.StrUtil;
import vben.core.module.sys.org.dept.SysOrgDept;
import vben.core.module.sys.org.dept.SysOrgDeptService;
import vben.core.module.sys.org.group.SysOrgGroup;
import vben.core.module.sys.org.group.SysOrgGroupCate;
import vben.core.module.sys.org.group.SysOrgGroupCateService;
import vben.core.module.sys.org.group.SysOrgGroupService;
import vben.core.module.sys.org.post.SysOrgPost;
import vben.core.module.sys.org.post.SysOrgPostService;
import vben.core.module.sys.org.role.*;
import vben.core.module.sys.org.root.SysOrg;
import vben.core.module.sys.org.user.SysOrgUser;
import vben.core.module.sys.org.user.SysOrgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

//组织架构初始化，可根据application.properties的app.init.orgType配置生成不同的基础数据
@Component
public class SysOrgInit {

    private String rootId = "1";

    protected void initRoot(String name,String id) {
        if(StrUtil.isNotBlank(id)){
            rootId=id;
        }
        SysOrgDept root = new SysOrgDept();
        root.setId(rootId);
        root.setName(name);
        root.setLabel("1");
        root.setOrnum(0);
        root.setAvtag(true);
        root.setTier("x"+rootId+"x");
        root.setType(1);
        deptService.insert(root);
    }

    //初始化管理员
    protected void initSa() {
        SysOrgUser user = new SysOrgUser();
        user.setId("sa");
        user.setUsnam("sa");
        user.setName("管理员");
        user.setPacod("1");
        user.setAvtag(true);
        user.setDept(new SysOrg(rootId));
        user.setTier("x"+rootId+"xsax");
        userService.insert(user);

        SysOrgUser user2 = new SysOrgUser();
        user2.setId("vben");
        user2.setUsnam("vben");
        user2.setName("小维");
        user2.setPacod("123456");
        user2.setAvtag(true);
        user2.setDept(new SysOrg(rootId));
        user2.setTier("x"+rootId+"xvbenx");
        userService.insert(user2);
    }

    //初始化测试部门与人员
    protected void initTestOrg() {
        SysOrgDept d1=new SysOrgDept();
        d1.setId("dept1");
        d1.setName("测试一部");
        d1.setLabel("1");
        d1.setOrnum(9991);
        d1.setAvtag(true);
        d1.setType(2);
        d1.setParent(new SysOrg(rootId));
        d1.setTier("x"+rootId+"xdept1x");
        deptService.insert(d1);

        SysOrgDept d2=new SysOrgDept();
        d2.setId("dept2");
        d2.setName("测试二部");
        d2.setLabel("1");
        d2.setOrnum(9992);
        d2.setAvtag(true);
        d2.setType(2);
        d2.setParent(new SysOrg(rootId));
        d2.setTier("x"+rootId+"xdept2x");
        deptService.insert(d2);

        List<SysOrgUser> userList=new ArrayList<>();
        SysOrgUser u1 = new SysOrgUser();
        u1.setId("l1");
        u1.setUsnam("l1");
        u1.setName("刘一");
        u1.setPacod("1");
        u1.setOrnum(1);
        u1.setAvtag(true);
        u1.setDept(new SysOrg("dept1"));
        u1.setTier("x"+rootId+"xdept1xl1x");
        userList.add(u1);

        SysOrgUser u2 = new SysOrgUser();
        u2.setId("c2");
        u2.setUsnam("c2");
        u2.setName("陈二");
        u2.setPacod("1");
        u2.setOrnum(2);
        u2.setAvtag(true);
        u2.setDept(new SysOrg("dept1"));
        u2.setTier("x"+rootId+"xdept1xc2x");
        userList.add(u2);

        SysOrgUser u3 = new SysOrgUser();
        u3.setId("z3");
        u3.setUsnam("z3");
        u3.setName("张三");
        u3.setPacod("1");
        u3.setOrnum(3);
        u3.setAvtag(true);
        u3.setDept(new SysOrg("dept1"));
        u3.setTier("x"+rootId+"xdept1xz3x");
        userList.add(u3);

        SysOrgUser u4 = new SysOrgUser();
        u4.setId("l4");
        u4.setUsnam("l4");
        u4.setName("李四");
        u4.setPacod("1");
        u4.setOrnum(4);
        u4.setAvtag(true);
        u4.setDept(new SysOrg("dept1"));
        u4.setTier("x"+rootId+"xdept1xl4x");
        userList.add(u4);

        SysOrgUser u5 = new SysOrgUser();
        u5.setId("w5");
        u5.setUsnam("w5");
        u5.setName("王五");
        u5.setPacod("1");
        u5.setOrnum(5);
        u5.setAvtag(true);
        u5.setDept(new SysOrg("dept2"));
        u5.setTier("x"+rootId+"xdept2xw5x");
        userList.add(u5);

        SysOrgUser u6 = new SysOrgUser();
        u6.setId("z6");
        u6.setUsnam("z6");
        u6.setName("赵六");
        u6.setPacod("1");
        u6.setOrnum(6);
        u6.setAvtag(true);
        u6.setDept(new SysOrg("dept2"));
        u6.setTier("x"+rootId+"xdept2xz6x");
        userList.add(u6);

        SysOrgUser u7 = new SysOrgUser();
        u7.setId("s7");
        u7.setUsnam("s7");
        u7.setName("孙七");
        u7.setPacod("1");
        u7.setOrnum(7);
        u7.setAvtag(true);
        u7.setDept(new SysOrg("dept2"));
        u7.setTier("x"+rootId+"xdept2xs7x");
        userList.add(u7);

        SysOrgUser u8 = new SysOrgUser();
        u8.setId("z8");
        u8.setUsnam("z8");
        u8.setName("周八");
        u8.setPacod("1");
        u8.setOrnum(8);
        u8.setAvtag(true);
        u8.setDept(new SysOrg("dept2"));
        u8.setTier("x"+rootId+"xdept2xz8x");
        userList.add(u8);

        userService.insertAll(userList);

        SysOrgPost p1 = new SysOrgPost();
        p1.setId("post1");
        p1.setName("一部负责人");
        p1.setOrnum(0);
        p1.setAvtag(true);
        p1.setDept(new SysOrg("dept1"));
        p1.setTier("x"+rootId+"xdept1xpost1x");
        p1.setUsers(new ArrayList<>());
        p1.getUsers().add(new SysOrg("l1","刘一",8));
        postService.insert(p1);

        SysOrgPost p2 = new SysOrgPost();
        p2.setId("post2");
        p2.setName("二部负责人");
        p2.setOrnum(0);
        p2.setAvtag(true);
        p2.setDept(new SysOrg("dept2"));
        p2.setTier("x"+rootId+"xdept2xpost2x");
        p2.setUsers(new ArrayList<>());
        p2.getUsers().add(new SysOrg("w5","王五",8));
        p2.getUsers().add(new SysOrg("z6","赵六",8));
        postService.insert(p2);

        SysOrgGroupCate gcate = new SysOrgGroupCate();
        gcate.setId("gcate");
        gcate.setName("测试群组分类");
        gcate.setAvtag(true);
        gcate.setOrnum(1);
        gcate.setCrman(new SysOrg("sa"));
        groupCateService.insert(gcate);

        SysOrgGroup g1 = new SysOrgGroup();
        g1.setId("group1");
        g1.setName("张三李四组");
        g1.setOrnum(1);
        g1.setAvtag(true);
        g1.setCatid("gcate");
        g1.setMembers(new ArrayList<>());
        g1.getMembers().add(new SysOrg("z3","张三",8));
        g1.getMembers().add(new SysOrg("l4","李四",8));
        groupService.insert(g1);

        SysOrgGroup g2 = new SysOrgGroup();
        g2.setId("group2");
        g2.setName("用户岗位部门混合组");
        g2.setOrnum(2);
        g2.setAvtag(true);
        g2.setCatid("gcate");
        g2.setMembers(new ArrayList<>());
        g2.getMembers().add(new SysOrg("c2","陈二",8));
        g2.getMembers().add(new SysOrg("post1","一部负责人",4));
        g2.getMembers().add(new SysOrg("dept2","测试二部",2));
        groupService.insert(g2);

        SysOrgRoleTree rtree1 = new SysOrgRoleTree();
        rtree1.setId("rtree1");
        rtree1.setName("业务角色树");
        rtree1.setOrnum(1);
        rtree1.setAvtag(true);
        rtree1.setRoles(new ArrayList<>());
        rtree1.getRoles().add(new SysOrgRole("role1","分管领导",1));
        rtree1.getRoles().add(new SysOrgRole("role2","部门领导",2));
        rtree1.setCrman(new SysOrg("sa"));
        roleTreeService.insertx(rtree1);

        SysOrgRoleNode rnode1 = new SysOrgRoleNode();
        rnode1.setId("rnode1");
        rnode1.setName("一部分管领导");
        rnode1.setTreid("rtree1");
        rnode1.setOrnum(1);
        rnode1.setAvtag(true);
        rnode1.setMember(new SysOrg("post1"));
        rnode1.setTier("xrnode1x");
        roleNodeService.insert(rnode1);

        SysOrgRoleNode rnode11 = new SysOrgRoleNode();
        rnode11.setId("rnode11");
        rnode11.setName("一部部门领导");
        rnode11.setTreid("rtree1");
        rnode11.setOrnum(1);
        rnode11.setAvtag(true);
        rnode11.setMember(new SysOrg("c2"));
        rnode11.setParent(new SysOrgRoleNode("rnode1"));
        rnode11.setTier("xrnode1xrnode11x");
        roleNodeService.insert(rnode11);

        SysOrgRoleNode rnode111 = new SysOrgRoleNode();
        rnode111.setId("rnode111");
        rnode111.setName("一部人员");
        rnode111.setTreid("rtree1");
        rnode111.setOrnum(1);
        rnode111.setAvtag(true);
        rnode111.setMember(new SysOrg("dept1"));
        rnode111.setParent(new SysOrgRoleNode("rnode11"));
        rnode111.setTier("xrnode1xrnode11xrnode111x");
        roleNodeService.insert(rnode111);

        SysOrgRoleNode rnode2 = new SysOrgRoleNode();
        rnode2.setId("rnode2");
        rnode2.setName("二部分管领导");
        rnode2.setTreid("rtree1");
        rnode2.setOrnum(2);
        rnode2.setAvtag(true);
        rnode2.setMember(new SysOrg("post2"));
        rnode2.setTier("xrnode2x");
        roleNodeService.insert(rnode2);

        SysOrgRoleNode rnode21 = new SysOrgRoleNode();
        rnode21.setId("rnode21");
        rnode21.setName("二部部门领导");
        rnode21.setTreid("rtree1");
        rnode21.setOrnum(1);
        rnode21.setAvtag(true);
        rnode21.setMember(new SysOrg("z6"));
        rnode21.setParent(new SysOrgRoleNode("rnode2"));
        rnode21.setTier("xrnode2xrnode21x");
        roleNodeService.insert(rnode21);

        SysOrgRoleNode rnode211 = new SysOrgRoleNode();
        rnode211.setId("rnode211");
        rnode211.setName("二部人员");
        rnode211.setTreid("rtree1");
        rnode211.setOrnum(1);
        rnode211.setAvtag(true);
        rnode211.setMember(new SysOrg("dept2"));
        rnode211.setParent(new SysOrgRoleNode("rnode21"));
        rnode211.setTier("xrnode2xrnode21xrnode211x");
        roleNodeService.insert(rnode211);

    }

    @Autowired
    private SysOrgRoleNodeService roleNodeService;

    @Autowired
    private SysOrgRoleTreeService roleTreeService;

    @Autowired
    private SysOrgGroupCateService groupCateService;

    @Autowired
    private SysOrgGroupService groupService;

    @Autowired
    private  SysOrgDeptService deptService;

    @Autowired
    private  SysOrgUserService userService;

    @Autowired
    private SysOrgPostService postService;

}
